#include "SQLSubPartitionByHash.h"
#include "../../sql/visitor/SQLASTVisitor.h"
#include "../../sql/ast/SQLExpr.h"
#include "../../utils/instanceof.h"
#include "SQLName.h"

SQLExpr_ptr SQLSubPartitionByHash::getExpr()
{
  return expr;
}

void SQLSubPartitionByHash::setExpr(SQLExpr_ptr expr)
{
  if (expr != nullptr)
  {
    expr->setParent(SharedObject(SQLSubPartitionByHash));
  }
  this->expr = expr;
}

// @Override
void SQLSubPartitionByHash::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLSubPartitionByHash)))
  {
    acceptChild(visitor, expr);
    acceptChild(visitor, subPartitionsCount);
  }
  visitor->endVisit(SharedObject(SQLSubPartitionByHash));
}

BOOL_ptr SQLSubPartitionByHash::isKey()
{
  return key;
}

void SQLSubPartitionByHash::setKey(BOOL_ptr key)
{
  this->key = key;
}

SQLObject_ptr SQLSubPartitionByHash::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<SQLSubPartitionByHash>(new SQLSubPartitionByHash());
  std::shared_ptr<SQLSubPartitionByHash> x = std::dynamic_pointer_cast<SQLSubPartitionByHash>(clone_tmp);
  // SQLSubPartitionByHash *x = new SQLSubPartitionByHash();
  if (expr != nullptr)
  {
    x->setExpr(std::dynamic_pointer_cast<SQLExpr>(expr->clone()));
  }
  x->key = key;
  return x;
}

BOOL_ptr SQLSubPartitionByHash::isPartitionByColumn(long columnNameHashCode64)
{
  return (instanceof <SQLExpr, SQLName>(expr) && (std::dynamic_pointer_cast<SQLName>(expr))->nameHashCode64() == columnNameHashCode64)?BOOL::TRUE:BOOL::FALSE;
}
